<?php

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class SchoolController extends AdminBaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $name = input('school_name');
        //链接数据库
        $school = Db::name('school')
            //表连接
            ->join ('cmf_area a','a.areaId = cmf_school.province','LEFT')
            ->join ('cmf_area b','b.areaId = cmf_school.city','LEFT')
            ->join ('cmf_area c','c.areaId = cmf_school.area','LEFT')
            //选择要输出的字段
            ->field('cmf_school.*,a.areaName a ,b.areaName b,c.areaName c')
            //id倒叙
            ->order('id desc')
            //每页显示多少条数据
            ->paginate('2', false ,['query'=>$this->request->param()]);
        //获取分页显示
        $page=$school->render();
        $this->assign('page',$page);
        $this->assign('school',$school);
        return $this->fetch('index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        //
        $province=Db::name('area')
            ->where('level',1)
            ->select();
        $this->assign('province',$province);
        return $this->fetch('add');
    }
    public function addPost()
    {
        if ($this->request->isPost()){
            //获取接受到的值
            $data=input();
            //判断是否为空

            if (empty($data['name'])){
                $this->error('学校名称不能为空');
            }
            //把数据同步到数据库
            $school=Db::name('school')->insert($data);
            //$area=Db::name('area')->insert($data);
            //判断是否添加成功
            if ($school){
                $this->success('添加成功','school/index');
            }else{
                $this->success('添加失败');
            }
        }
    }
    //城市和市区联动
    public function getarea(){
        $areaId=input('areaId');
        $city=Db::name('area')
            ->where(['parentId'=>$areaId])
            ->all();
        $options=["<option value=''>--请选择所在城市--</option>"];
        if ($city){
            foreach ($city as $v){
                $opt='<option value="'.$v['areaId'].'">'.$v['areaName'].'</option>';
                array_push($options,$opt);
            }
            return $options;
        }else{
            return["<option value=''>--暂无数据--</option>"];
        }
    }
    //区和县的联动
    public function getcity(){
        $areaId=input('areaId');
        $city=Db::name('area')
            ->where(['parentId'=>$areaId])
            ->all();
        $options=["<option value=''>--请选择所在区县--</option>"];
        if ($city){
            foreach ($city as $v){
                $opt='<option value="'.$v['areaId'].'">'.$v['areaName'].'</option>';
                array_push($options,$opt);
            }
            return $options;
        }else{
            return["<option value=''>--暂无数据--</option>"];
        }
    }
    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        //查询School表的数据进行剪辑
        $id = $this->request->param('id', 0, 'intval');
        $school = DB::name('school')->where("id", $id)->find();
        $this->assign('school', $school);
        return $this->fetch('edit');
    }
    public function editPost()
    {
        if ($this->request->isPost()) {
            //接受传过来的值
            $data = input();
            //把数据同步到数据库
            $edit = Db::name('school')->update($data, ['id' => $data['id']]);
            //判断
            if ($edit) {
                $this->success('编辑成功', 'school/index');
            } else {
                $this->success('编辑失败');
            }

        }
    }
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        //接收ID
        $id=$this->request->param('id','0','intval');
        //选中ID进行删除
        if (Db::name('school')->delete('id')!== false){
            Db::name('school')->where("id",$id)->delete();
            $this->success('删除成功','school/index');
        }else{
            $this->success('删除失败！');
        }
    }
}
